package com.game.centerserver.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.game.centerserver.common.Result;
import com.game.centerserver.infrastructure.entity.User;
import com.game.centerserver.infrastructure.service.UserService;
import com.game.centerserver.pojo.result.UserLoginResult;
import com.game.centerserver.service.UserServiceService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2024-11-13 21:32:20
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * user服务对象
     */
    @Resource
    private UserServiceService userServiceService;
    @Resource
    private UserService userService;

    @PostMapping("/doLogin")
    public Result<UserLoginResult> doLogin(String iphoneNumber, String password){
        UserLoginResult userLoginResult = new UserLoginResult();
        Result<SaTokenInfo> res = userServiceService.doLogin(iphoneNumber, password);
        if (!res.getSuccess()) {
            userLoginResult.setUid("-1");
            userLoginResult.setTokenInfo(res.getData());
            return Result.fail(res.getMessage(), userLoginResult);
        }
        User user = userService.queryByIphoneNumber(iphoneNumber);
        userLoginResult.setUid(user.getUid());
        userLoginResult.setTokenInfo(res.getData());
        return Result.ok(res.getMessage(), userLoginResult);
    }

    @PostMapping("/register")
    public Result register(String iphoneNumber, String password){
        return userServiceService.register(iphoneNumber, password);
    }

    @GetMapping("/logout")
    public Result logout(){
        return userServiceService.logout();
    }

    @DeleteMapping("/deleteUserByUid")
    public Result deleteUserByUid(String uid){
        return userServiceService.deleteUserByUid(uid);
    }
}

